import { clearUserInfo } from '@/utils/util'
import ls from '@/utils/Storage'
import { ACCESS_TOKEN, MENU_NAV, PERMISSION, ROLE, USER_INFO } from '@/store/mutation-types'
import { notification } from 'ant-design-vue'
import { Router } from 'vue-router'
import { GET } from '@/api/api'
import generateAsyncRoutes from '@/router/generateAsyncRoutes'
export const loginSuccess = (res, router: Router) => {
  const promise = new Promise((resolve, reject) => {
    if (res && res.data) {
      ls.set(ACCESS_TOKEN, res.data)
    }
    const all = [
      GET('/authority/security/permissionSet'),
      GET('/realm/mine/info'),
      GET('/menu/mine/list?app=portal')
    ]
    Promise.all(all)
      .then((allRes) => {
        ls.set(PERMISSION, allRes[0].data.permissions)
        ls.set(ROLE, allRes[0].data.roles)
        ls.set(USER_INFO, allRes[1].data)
        ls.remove(MENU_NAV)
        generateAsyncRoutes(router, { portal: allRes[2].data })
        resolve(allRes)
        // router.push({ path: '/' })
      })
      .catch((e) => {
        clearUserInfo()
        reject(e)
      })
  })
  return promise
}

export const requestFailed = (err) => {
  notification['error']({
    message: '错误',
    description: ((err.response || {}).data || {}).message || '请求出现错误，请稍后再试',
    duration: 4
  })
}
